package com.huawei.hms.framework.network.grs;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.grs.a.a;
import com.huawei.hms.framework.network.grs.c.b;
import com.huawei.hms.framework.network.grs.local.c;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class GrsClient {
    private static final int DEFAULT_TIME_OUT = 10;
    private static final long REQUEST_BLOCK_TIME = 604800000;
    private static final String SPKEY_UNION_SUFFIX = "time";
    private static final String TAG = GrsClient.class.getSimpleName();
    private ExecutorService executorService = Executors.newSingleThreadExecutor();
    private Future<Boolean> future;
    private GrsBaseInfo grsBaseInfo;
    private boolean isInit = false;
    private final Object lock = new Object();

    public GrsClient(Context context, GrsBaseInfo grsBaseInfo) {
        if (context == null || grsBaseInfo == null || TextUtils.isEmpty(grsBaseInfo.getAppName())) {
            throw new NullPointerException("invalid init params for context is null or {appname} set null or empty string.");
        }
        b.a(context.getApplicationContext());
        try {
            this.grsBaseInfo = grsBaseInfo.clone();
        } catch (Throwable e) {
            Logger.w(TAG, "GrsClient catch CloneNotSupportedException", e);
            this.grsBaseInfo = grsBaseInfo.copy();
        }
        if (!this.isInit) {
            synchronized (this.lock) {
                if (!this.isInit) {
                    final GrsBaseInfo grsBaseInfo2 = this.grsBaseInfo;
                    this.future = this.executorService.submit(new Callable<Boolean>(this) {
                        final /* synthetic */ GrsClient b;

                        public Boolean a() {
                            if (!c.a().a(grsBaseInfo2)) {
                                com.huawei.hms.framework.network.grs.local.b.a().a(grsBaseInfo2);
                            }
                            this.b.autoClear(com.huawei.hms.framework.network.grs.a.c.a().d());
                            a.a(grsBaseInfo2);
                            return Boolean.valueOf(this.b.isInit = true);
                        }

                        public /* synthetic */ Object call() {
                            return a();
                        }
                    });
                }
            }
        }
    }

    private void autoClear(Map<String, ?> map) {
        if (map == null || map.isEmpty()) {
            Logger.v(TAG, "sp's content is empty.");
            return;
        }
        for (String str : map.keySet()) {
            if (str.endsWith(SPKEY_UNION_SUFFIX)) {
                long j;
                String a = com.huawei.hms.framework.network.grs.a.c.a().a(str, "");
                if (TextUtils.isEmpty(a) || !a.matches("\\d+")) {
                    j = 0;
                } else {
                    try {
                        j = Long.parseLong(a);
                    } catch (Throwable e) {
                        Logger.w(TAG, "convert expire time from String to Long catch NumberFormatException.", e);
                        j = 0;
                    }
                }
                if (!isValid(j)) {
                    Logger.i(TAG, "init interface auto clear some invalid sp's data.");
                    com.huawei.hms.framework.network.grs.a.c.a().a(str.substring(0, str.length() - SPKEY_UNION_SUFFIX.length()));
                    com.huawei.hms.framework.network.grs.a.c.a().a(str);
                }
            }
        }
    }

    private boolean isInitialized() {
        try {
            return this.future != null ? ((Boolean) this.future.get(10, TimeUnit.SECONDS)).booleanValue() : false;
        } catch (Throwable e) {
            Logger.w(TAG, "init compute task canceled.", e);
            return false;
        } catch (Throwable e2) {
            Logger.w(TAG, "init compute task failed.", e2);
            return false;
        } catch (Throwable e22) {
            Logger.w(TAG, "init compute task interrupted.", e22);
            return false;
        } catch (TimeoutException e3) {
            Logger.w(TAG, "init compute task timed out");
            return false;
        } catch (Throwable e222) {
            Logger.w(TAG, "init compute task occur unknown Exception", e222);
            return false;
        }
    }

    private boolean isValid(long j) {
        return System.currentTimeMillis() - j <= 604800000;
    }

    public void ayncGetGrsUrl(String str, String str2, IQueryUrlCallBack iQueryUrlCallBack) {
        if (iQueryUrlCallBack == null) {
            Logger.w(TAG, "IQueryUrlCallBack is must not null for process continue.");
        } else if (this.grsBaseInfo == null || str == null || str2 == null) {
            iQueryUrlCallBack.onCallBackFail(-6);
        } else if (isInitialized()) {
            new a(this.grsBaseInfo).a(str, str2, iQueryUrlCallBack);
        }
    }

    public void ayncGetGrsUrls(String str, IQueryUrlsCallBack iQueryUrlsCallBack) {
        if (iQueryUrlsCallBack == null) {
            Logger.w(TAG, "IQueryUrlsCallBack is must not null for process continue.");
        } else if (this.grsBaseInfo == null || str == null) {
            iQueryUrlsCallBack.onCallBackFail(-6);
        } else if (isInitialized()) {
            new a(this.grsBaseInfo).a(str, iQueryUrlsCallBack);
        }
    }

    public boolean forceExpire() {
        if (!isInitialized() || this.grsBaseInfo == null || b.a() == null) {
            return false;
        }
        a.b(this.grsBaseInfo);
        return true;
    }

    public String synGetGrsUrl(String str, String str2) {
        if (this.grsBaseInfo != null && str != null && str2 != null) {
            return isInitialized() ? new a(this.grsBaseInfo).a(str, str2) : null;
        } else {
            Logger.w(TAG, "invalid para!");
            return null;
        }
    }

    public Map<String, String> synGetGrsUrls(String str) {
        if (this.grsBaseInfo != null && str != null) {
            return isInitialized() ? new a(this.grsBaseInfo).a(str) : new HashMap();
        } else {
            Logger.w(TAG, "invalid para!");
            return new HashMap();
        }
    }
}
